package jhavelogger.data;

import java.util.LinkedHashMap;
import java.util.Map;

import com.maxmind.geoip.Location;

/**
 * Caches GeoIP location objects in an in-memory cache. Least Recently Used
 * objects will be automatically removed.
 * 
 * @author Jared Schlicht
 */
public class LocationCache extends LinkedHashMap<String,Location> {
    private int cacheSize;
    
    /**
     * Constructs a new LocationCache
     * @param cacheSize number of items to cache
     */
    public LocationCache(int cacheSize) {
	// Least recently used items are removed first
	super(cacheSize, 0.75f, true);
	this.cacheSize = cacheSize;
    }
    
    /**
     * Specifies that old items should be removed once this collection contains
     * too many entries.
     * @param eldest not used
     * @return true if the oldest item should be removed
     */
    @Override
    protected boolean removeEldestEntry(Map.Entry<String,Location> eldest) {
	return size() > cacheSize;
    }
}
